package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.Dictionary;

	public class Box extends Sprite
	{
		private var nextColumn:uint;
		private var VALUES:Array;
		
		private var _hasFlashing:Boolean;
		
		public function Box()
		{
			// c x r
			VALUES = [[],[],[]];
			this.graphics.beginFill(0xFFFF00, 0.5);
			this.graphics.drawRect(-10, -10, 190, 190);
			this.graphics.endFill();
			
			this.buttonMode = true;
		}
		
		public function get hasFlashing():Boolean
		{
			return _hasFlashing;
		}

		public function flashPotetionalWin(potentialColumn:Array):void
		{
			if (full()) return;
			
			for (var i:int = 0, len:int = potentialColumn.length; i < len; i++)
			{
				var rowMatchValue:String = potentialColumn[i];
				var firstDice:RandomDice;
				var secondDice:RandomDice;
				
				if (nextColumn == 2)
				{
					firstDice = (VALUES[0][i] as RandomDice);
					secondDice = (VALUES[1][i] as RandomDice);
					if (firstDice.diceValue() == secondDice.diceValue() ==  rowMatchValue)
					{
						_hasFlashing = true;
						firstDice.startFlash();
						secondDice.startFlash();
					}
				}else if (nextColumn == 1)
				{
					firstDice = (VALUES[0][i] as RandomDice);
					if (firstDice.diceValue() == rowMatchValue)
					{
						_hasFlashing = true;
						firstDice.startFlash();
					}
					
				}
			}
		}
		
		public function stopFlash():void
		{
			if (!_hasFlashing) return;
			
			for (var i:int = 0, len:int = VALUES.length; i < len; i++)
			{
				var column:Array = VALUES[i];
				for (var j:int = 0, lenj:int = column.length; j < lenj; j++)
				{
					var dice:RandomDice = VALUES[i][j] as RandomDice;
					dice.stopFlash();
				}
			}
			
			_hasFlashing = false;
		}
		
		public function addResult(resultColumn:Array):void
		{
			if (full()) return;
			
			var x:Number = nextColumn*60;
			
			var one:RandomDice = new RandomDice();
			one.x = x;
			one.y = 0;
			one.setValue(resultColumn[0]);
			VALUES[nextColumn][0] = one;
			this.addChild(one);
			
			var two:RandomDice = new RandomDice();
			two.x = x;
			two.y = 60;
			two.setValue(resultColumn[1]);
			VALUES[nextColumn][1] = two;
			this.addChild(two);
			
			var three:RandomDice = new RandomDice();
			three.x = x;
			three.y = 120;
			three.setValue(resultColumn[2]);
			VALUES[nextColumn][2] = three;
			this.addChild(three);
			
			nextColumn++;
		}
		
		public function full():Boolean
		{
			return (nextColumn >= 3);	
		}
	}
}